package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlComputingResource;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlResourceStorage;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MGlResourceStorageMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlResourceStorageService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 计算机存储关系表
 * @Author: xiaoyq
 * @Date:   2023-07-03
 * @Version: V1.0
 */
@Service
public class MGlResourceStorageServiceImpl extends ServiceImpl<MGlResourceStorageMapper, MGlResourceStorage> implements IMGlResourceStorageService {

    @Override
    public void batchDealData(String category,String resourceId,String storageIds) {
        //提取修改前存储数据
        QueryWrapper<MGlResourceStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MGlResourceStorage::getResourceId,resourceId);
        List<MGlResourceStorage> computingStorages = this.list(queryWrapper);
        String nIds = storageIds==null? "":storageIds;
        String oIds = "";
        UpdateWrapper<MGlResourceStorage> updateWrapper = new UpdateWrapper<>();
        MGlResourceStorage item;
        for (int i = 0; i < computingStorages.size(); i++) {
            item = computingStorages.get(i);
            oIds = oIds + "," + item.getStorageId();
            String sId = item.getStorageId();
            if(!nIds.contains(item.getStorageId())){
                //删除StorageId
                updateWrapper.lambda().or(qr->qr.eq(MGlResourceStorage::getCategory,category).eq(MGlResourceStorage::getStorageId,sId).eq(MGlResourceStorage::getResourceId,resourceId));
            }
        }
        List<String> idList = Arrays.asList(nIds.split(","));
        List<MGlResourceStorage> insertDataList = new ArrayList<>();
        for (int i = 0; i < idList.size(); i++) {
            if(!oIds.contains(idList.get(i))){
                //添加数据项
                item = new MGlResourceStorage();
                item.setResourceId(resourceId);
                item.setCategory(category);
                item.setStorageId(idList.get(i));
                insertDataList.add(item);
            }
        }
        if(!insertDataList.isEmpty()){
            this.saveBatch(insertDataList);
        }
        if(!updateWrapper.isEmptyOfWhere()){
            this.remove(updateWrapper);
        }
    }

    @Override
    public boolean batchRemoveStorage(String category, String resourceId) {
        UpdateWrapper<MGlResourceStorage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MGlResourceStorage::getCategory,category).eq(MGlResourceStorage::getResourceId,resourceId);
        return this.remove(updateWrapper);
    }

    @Override
    public boolean batchRemoveStorageByResIds(String category, List<String> idList) {
        UpdateWrapper<MGlResourceStorage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MGlResourceStorage::getCategory,category).in(MGlResourceStorage::getResourceId,idList);
        return this.remove(updateWrapper);
    }

    @Override
    public boolean forceRemoveStorage(String storageId) {
        UpdateWrapper<MGlResourceStorage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MGlResourceStorage::getStorageId,storageId);
        return this.remove(updateWrapper);
    }

    @Override
    public boolean forceRemoveStorageByIds(List<String> idList) {
        UpdateWrapper<MGlResourceStorage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().in(MGlResourceStorage::getStorageId,idList);
        return this.remove(updateWrapper);
    }
}
